﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Impinj.OctaneSdk;
using RFIDStockManagement.Common.Public.Enums;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.Common.Public;

namespace IOReadWriter
{
    public class RfidSpeedwayReader
    {
        private SpeedwayReader m_Reader = null;
        //private EnumIOStockOperate m_EnumIOStockOperate = EnumIOStockOperate.InStock;
        private string m_ReaderName = string.Empty;
        public RfidSpeedwayReader(string readerName, string strAntennaList, EnumIOStockOperate enmOperate)
        {
            m_ReaderName = readerName;
            //m_EnumIOStockOperate = enmOperate;
            m_Reader = new SpeedwayReader();
            try
            {
                m_Reader.Connect(readerName);
            }
            catch (ConnectException cnnEx)
            {
                m_Reader = null;
            }
            if (m_Reader != null)
            {
                m_Reader.ClearSettings();
                Settings settings = m_Reader.QueryFactorySettings();
                settings.Report.IncludeAntennaPortNumber = true;
                settings.Report.Mode = ReportMode.Individual;

                //ListOfAntennaSettings antennaList = new ListOfAntennaSettings();
                //string[] portNumber = strAntennaList.Split(',');
                //foreach (string number in portNumber)
                //{
                //    AntennaSettings antenna = new AntennaSettings();
                //    antenna.PortNumber = int.Parse(number);
                //    antennaList.Add(antenna);
                //}
                //settings.Antennas = antennaList;

                settings.Filters.Mode = TagFilterMode.OnlyFilter1;
                settings.Filters.TagFilter1.MemoryBank = MemoryBank.Epc;
                settings.Filters.TagFilter1.BitPointer = 32;//0x20;
                settings.Filters.TagFilter1.BitCount = PublicConst.TagSysMarkBit * 4;
                settings.Filters.TagFilter1.TagMask = PublicConst.TagSysMark;
                settings.Filters.TagFilter1.FilterOp = TagFilterOp.Match;

                m_Reader.ApplySettings(settings);
                m_Reader.TagsReported += new EventHandler<TagsReportedEventArgs>(OnTagsReported);
            }

        }

        public void StartRead()
        {
            if (m_Reader != null && !m_Reader.QueryStatus().IsSingulating)
            {
                m_Reader.Start();
            }
        }
        public void StopRead(bool disConnect)
        {
            if (m_Reader != null)
            {
                if (m_Reader.QueryStatus().IsSingulating)
                {
                    m_Reader.Stop();
                }
                if (disConnect)
                {
                    m_Reader.Disconnect();
                }
            }
        }

        private void OnTagsReported(object sender, TagsReportedEventArgs args)
        {
            List<string> lstRfid = new List<string>();

            foreach (var item in args.TagReport.Tags)
            {

                RfidCacheManager.WriteRfidDataInfo(item.Epc, SpeedwayPublicFunction.CheckIOStockOperate(m_ReaderName, item.AntennaPortNumber));

            }
        }
    }
}
